package com.itboye.sunsun.adapter;

import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.beans.ShopCarBean;
import com.itboye.sunsun.shop.ui.ShopCarActivity;
import com.itboye.sunsun.utils.IntegerParse;
import com.itboye.sunsun.utils.XImageLoader;

import android.app.Application;
import android.content.Context;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShopCarAdapter extends BaseAdapter {

	Context context;
	 List<ShopCarBean> dataList;
	 ShopCarActivity shopCarActivity;
	public ShopCarAdapter(Context context, List<ShopCarBean> dataList,ShopCarActivity shopCarActivity) {
		this.context=context;
		this.dataList=dataList;
		this.shopCarActivity=shopCarActivity;
	}

	@Override
	public int getCount() {
		
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		
		return null;
	}

	@Override
	public long getItemId(int position) {
		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			
			convertView= LayoutInflater.from(context).inflate(R.layout.item_shop_car	, null);
			holder=new ViewHolder();
			holder.isSelected=  convertView.findViewById(R.id.isSelected);
			holder.name=(TextView) convertView.findViewById(R.id.name);
			holder.img=(ImageView) convertView.findViewById(R.id.img);
			holder.guige=(TextView) convertView.findViewById(R.id.guige);
			holder.jiage=(TextView)convertView.findViewById(R.id.jiage);
			holder.jia=(TextView) convertView.findViewById(R.id.jia);
			
			holder.jian=(TextView) convertView.findViewById(R.id.jian);
			holder.input=(TextView) convertView.findViewById(R.id.input);
			convertView.setTag(holder);
			
			 
			 
			holder.isSelected.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int posi=(Integer) v.getTag();
					ShopCarBean bean= dataList.get(posi);
					bean.setSelected(!bean.isSelected());
					if (bean.isSelected()) {
						v.setSelected(true);
					}else{
						v.setSelected(false);
					}
					shopCarActivity.calculatePrice();
				}
			});
			
			holder.jia.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int posi=(Integer) v.getTag();
					TextView editText= (TextView) ((ViewGroup)v.getParent()).getChildAt(1);
					int count=IntegerParse.parse(dataList.get(posi).getCount(), 0);
					count++;
					editText.setText(count+"");
					dataList.get(posi).setCount(count+"");
					shopCarActivity.calculatePrice();
					
				}
			});
			
			holder.jian.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int posi=(Integer) v.getTag();
					TextView editText= (TextView) ((ViewGroup)v.getParent()).getChildAt(1);
					int count=IntegerParse.parse(dataList.get(posi).getCount(), 0);
					count--;
					if (count<1) {
						count=1;
					}
					editText.setText(count+"");
					dataList.get(posi).setCount(count+"");
					shopCarActivity.calculatePrice();
				}
			});
			
			 
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		
		ShopCarBean shopCarBean=dataList.get(position);
		
		XImageLoader.load(shopCarBean.getIconUrl(), holder.img);
		holder.name.setText(shopCarBean.getName());
		holder.guige.setText(shopCarBean.getSkuDesc());
		
		holder.jiage.setText(shopCarBean.getPrice());
		
		holder.isSelected.setTag(position);
		
		if (shopCarBean.isSelected()) {
			holder.isSelected.setSelected(true);
		}else{
			holder.isSelected.setSelected(false);
		}
		holder.input.setText(shopCarBean.getCount()+"");
		
		 
		holder.jia.setTag(position);
		holder.jian.setTag(position);
		holder.input.setTag(position);
		return convertView;
	}

	 
	class ViewHolder{
		View isSelected;
		TextView name;
		ImageView img;
		TextView guige;
		TextView jiage;
		TextView jia;
		TextView jian;
		TextView input;
		
	}
	
	 
}
